flyui.namespace("flyui.apps.expressionbygenebatch");

/**
 * @class
 * @constructor
 */
flyui.apps.expressionbygenebatch.Application = function() {
	
	var that = this;
	
	var batchgenefinder = null;
    var batchbdgp = null;
    var batchflyatlas = null;
    var batchflyted = null;
	
	
	this.initBatchGeneFinder = function() {
	    var context = "flyui.apps.expressionbygenebatch.Application.initBatchGeneFinder";
	    try {

            flyui.debug("instantiate a service for the batchgenefinder widget", context);
            var service = new flyui.flybase.Service("../../../data/flybase");
            
            flyui.debug("instantiate a renderer for the batchgenefinder widget", context);
            var renderPane = $("#batchGeneFinderWidget");
            var renderer = new flyui.genefinder.BatchWidget.DefaultRenderer(); // default renderer
            renderer.setCanvas(renderPane);
            
            flyui.debug("instantiate a genefinder widget", context);
            batchgenefinder = new flyui.genefinder.BatchWidget(service, renderer);
	        
        } catch (error) {
            flyui.apps.expressionbygenebatch.handleError(error);
        }
	};
		
    this.initBatchBdgp = function() {
        var context = "flyui.apps.expressionbygenebatch.Application.initBatchBdgp";
        try {

            flyui.debug("instantiate a service for the bdgp batch widget", context);
            var service = new flyui.bdgp.Service();
            service.setEndpoint("../../../data/bdgp");
            
            flyui.debug("instantiate a renderer for the bdgp batch widget", context);
            var renderPane = $("#batchBdgpImageWidget");
            var renderer = new flyui.bdgp.BatchWidget.DefaultRenderer(); // default renderer
            renderer.setCanvas(renderPane);
            
            flyui.debug("instantiate a bdgp widget", context);
            batchbdgp = new flyui.bdgp.BatchWidget(service, renderer);
            
        } catch (error) {
            flyui.apps.expressionbygenebatch.handleError(error);
        }
    };
    
    this.initBatchFlyted = function() {
        var context = "flyui.apps.expressionbygenebatch.Application.initBatchFlyted";
        try {

            flyui.debug("instantiate a service for the flyted batch widget", context);
            var service = new flyui.flyted.Service();
            service.setEndpoint("../../../data/flyted");
            
            flyui.debug("instantiate a renderer for the flyted batch widget", context);
            var renderPane = $("#batchFlytedImageWidget");
            var renderer = new flyui.flyted.BatchWidget.DefaultRenderer(); // default renderer
            renderer.setCanvas(renderPane);
            
            flyui.debug("instantiate a flyted widget", context);
            batchflyted = new flyui.flyted.BatchWidget(service, renderer);
            
        } catch (error) {
            flyui.apps.expressionbygenebatch.handleError(error);
        }
    };
        
    this.initBatchFlyatlas = function() {
        var context = "flyui.apps.flyatlasbygenebatch.Application.initBatchFlyatlas";
        try {

            flyui.debug("instantiate a service for the batchgenefinder widget", context);
            var service = new flyui.flyatlas.Service();
            service.setEndpoint("../../../data/flyatlas");
            
            flyui.debug("instantiate a renderer for the batchgenefinder widget", context);
            var renderPane = $("#batchFlyatlasWidget");
            var renderer = new flyui.flyatlas.BatchWidget.DefaultRenderer(); // default renderer
            renderer.setCanvas(renderPane);
            
            flyui.debug("instantiate a flyatlas widget", context);
            batchflyatlas = new flyui.flyatlas.BatchWidget(service, renderer);
            
        } catch (error) {
            flyui.apps.flyatlasbygenebatch.handleError(error);
        }
    };
        
	this.init = function() {
        var context = "flyui.apps.expressionbygenebatch.Application.init";
        try {
    		
    		var logReader = new YAHOO.widget.LogReader("logger");

            this.initBatchGeneFinder();
            this.initBatchBdgp();
            this.initBatchFlyatlas();
            this.initBatchFlyted();

            flyui.debug("hook form submission to widget call", context);
            YAHOO.util.Event.addListener("queryForm", "submit", this.onFormSubmitted);
            
            flyui.debug("add listener for genes found event", context);
            batchgenefinder.subscribe("GENESFOUND", this.onGenesFound, null);
            
    		flyui.debug("application loaded, showing main pane", context);
    		$("#loadingPane").hide();
    		$("#applicationPane").show();

            this.initState();
    		
        } catch (error) {
            flyui.apps.expressionbygenebatch.handleError(error);
        }
	};
	
	this.onFormSubmitted = function(event) {
        var context = "flyui.apps.expressionbygenebatch.Application.onFormSubmitted";
        try {
            flyui.debug("form submitted", context);
            $("#errorMessage")
               .hide()
               .html("this should never be visible");
            var query = document.getElementById("queryInput").value;
            that.setQueryState(query);
            that.doQuery(query);

        } catch (error) {
            flyui.apps.expressionbygenebatch.handleError(error);
        }
	    
	};
	
	this.doQuery = function(query) {
        var context = "flyui.apps.expressionbygenebatch.Application.doQuery";
        try {
            flyui.debug("raw query: "+query, context);
//            document.getElementById("queryInput").value = "";

            flyui.debug("strip out newlines, commas, quotes, ...", context);
            query = query.replace(/[,'"\n\s]/g, " ");
            
            var names = query.split(" ");
            var cleannames = [];
            for (var i=0; i<names.length; i++) {
                var n = names[i];
                if (n.length > 0) {
                    cleannames[cleannames.length] = n;
                }
            }
            flyui.debug("cleaned names: "+cleannames, context);
            batchgenefinder.findGenesByAnyNameBatch(cleannames);
            $("#batchBdgpImageWidget").hide();
            $("#batchFlyatlasWidget").hide();
            $("#batchFlytedImageWidget").hide();
            batchflyatlas.clear();
            batchbdgp.clear();	    
            batchflyted.clear();	    
        } catch (error) {
            flyui.apps.expressionbygenebatch.handleError(error);
        }
	}
	
    this.setQueryState = function(query) {
        try {
            var url = new String(window.location.href);
            var term = url.indexOf("#");
            if (term > 0) {
                url = url.substring(0, term);
            }
            window.location.href = url + "#query=" + escape(query);
        } catch (error) {
            flyui.apps.expressionbygenebatch.handleError(error);
        }
    };
    
    this.initState = function() {
        try {
    
            var url = new String(window.location.href);
            var qi = url.indexOf("#query=");
            
            if (qi > 0) {
                var end = url.length;
                var query = unescape(url.substring(qi+7,end));
                flyui.info("found query: "+query);
                document.getElementById("queryInput").value = query;
                that.doQuery(query);
            }
            
        } catch (error) {
            flyui.apps.expressionbygenebatch.handleError(error);
        }
    };

    this.onGenesFound = function(type, args) {
        try {
            flyui.debug("application: genes found");
            var querynames = args[0];
            var results = args[1];

            flyui.debug("query names: "+querynames);
            
            var foundnames = [];
            var foundids = [];
            var foundgenes = [];
            
            for (var i=0;i<querynames.length;i++) {
                if (results[querynames[i]].length == 1) {
                    foundnames[foundnames.length] = querynames[i];
                    foundids[foundids.length] = results[querynames[i]][0].flybaseID;
                    foundgenes[foundgenes.length] = results[querynames[i]][0];
                }
            }
            
            flyui.debug("found query names: "+foundnames);
            flyui.debug("found genes: "+foundgenes);
            
            if (foundnames.length > 0) {
                batchbdgp.findImagesByGeneBatch(foundnames, foundgenes);
                $("#batchBdgpImageWidget").show();
                batchflyatlas.getAssaysByGeneBatch(foundnames, foundids);
                $("#batchFlyatlasWidget").show();
                batchflyted.findImagesByGeneBatch(foundnames, foundgenes);
                $("#batchFlytedImageWidget").show();
            }
            
        } catch (error) {
            flyui.apps.expressionbygenebatch.handleError(error);
        }
    }
	
	
	this.init();
};

flyui.apps.expressionbygenebatch.handleError = function(error) {
    flyui.err(error.name+" :: "+error.message);
    $("#errorMessage")
       .html("an error has occurred, please see the log for more details")
       .show();
};
    

// on dom ready, instantiate application
YAHOO.util.Event.onDOMReady(function() {new flyui.apps.expressionbygenebatch.Application();});